var gulp = require('gulp'),
    replace = require('gulp-replace'),
    header = require('gulp-header'),
//autoprefixer = require('gulp-autoprefixer'),
    uglify = require('gulp-uglify');
minifycss = require('gulp-minify-css'),
    jshint = require('gulp-jshint'),
    //imagemin = require('gulp-imagemin'),
    //rename = require('gulp-rename'),
    clean = require('gulp-clean'),
    watch = require('gulp-watch'),
    concat = require('gulp-concat'),
    //notify = require('gulp-notify'),
    livereload = require('gulp-livereload');

function currentTime() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var hh = now.getHours();
    var mm = now.getMinutes();
    var ss = now.getSeconds();
    var clock = year + "-";
    if (month < 10)
        clock += "0";
    clock += month + "-";
    if (day < 10)
        clock += "0";
    clock += day + " ";
    if (hh < 10)
        clock += "0";
    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm;
    clock += ":";
    if (ss < 10) clock += '0';
    clock += ss;
    return (clock);
}
var pkg = require('./package.json');
var banner = ['/*',
    ' * Build by <%= pkg.author.name %>',
    ' * Date: ' + currentTime() + '',
    ' * Version: <%= pkg.appVersion %>',
    ' */',
    ''].join('\n');
var srcRoot = "src";
var srcPath = {
    base:pkg.host + "/" + pkg.appName + "/"+srcRoot,
    html: srcRoot +"/"+ "html/**",
    script: srcRoot +"/"+"js/**/*.js",
    css: srcRoot +"/"+"css/**/*.css",
    images: srcRoot +"/"+"images/**"
}
var buildRoot = "build/"+pkg.appVersion;
var buildPath = {
    base:pkg.host + "/" + pkg.appName + "/build/" + pkg.appVersion,
    html: buildRoot +"/"+ "html/",
    script: buildRoot +"/"+ "js/",
    css: buildRoot +"/"+ "css/",
    images: buildRoot +"/"+ "images/"
}
gulp.task('clean', function() {
    return gulp.src(['dest/'], {read: false})
        .pipe(clean());
});
/*html*/
gulp.task('html', function () {
    gulp.src(srcPath.html)
        .pipe(replace(srcPath.base,buildPath.base))
        .pipe(gulp.dest(buildPath.html));
});
/*script*/
gulp.task('script', function () {
    gulp.src(srcPath.script)
        .pipe(uglify())
        .pipe(replace(srcPath.base,buildPath.base))
        .pipe(header(banner, {pkg: pkg}))
        .pipe(gulp.dest(buildPath.script))

});

/*css*/
gulp.task('css', function () {
    gulp.src(srcPath.css)
        .pipe(minifycss())
        .pipe(header(banner, {pkg: pkg}))
        .pipe(replace(srcPath.base, buildPath.base))
        .pipe(gulp.dest(buildPath.css))
});

/*images*/
gulp.task('images', function () {
    gulp.src(srcPath.images)
        .pipe(gulp.dest(buildPath.images))

});
/*imagesmin*/
gulp.task('imagesmin', function () {
    gulp.src(srcPath.images)
        .pipe(imagemin({
            progressive: true
        }))
        .pipe(gulp.dest(buildPath.images))

});
gulp.task('live', function () {
    livereload.listen();
    gulp.watch([srcPath.html,srcPath.css,srcPath.script]).on('change', livereload.changed);
})
/*watch*/
gulp.task('watch', function () {
    gulp.watch(srcPath.css, ['css']);
    gulp.watch(srcPath.script, ['script']);
});

/*single html*/
gulp.task('singlehtml', function () {
    gulp.src([srcPath+'/html/eqs/base_info.html'])
        .pipe(replace(srcPath.base,buildPath.base))
        .pipe(gulp.dest('build/'+pkg.appVersion+'/html/eqs'));

});

/*single css*/
gulp.task('singlecss', function () {
    gulp.src(['src/css/animate.css'])
        .pipe(minifycss())
        .pipe(header(banner, {pkg: pkg}))
        .pipe(replace(srcPath.base, buildPath.base))
        .pipe(gulp.dest('build/'+pkg.appVersion+'/css/'))

});
/*single script*/
gulp.task('singlejs', function () {
    gulp.src(['src/js/notice_qa.js'/*,'src/js/bm_common.js'*/])
        .pipe(uglify())
        .pipe(replace(srcPath.base,buildPath.base))
        .pipe(header(banner, {pkg: pkg}))
        .pipe(gulp.dest('build/'+pkg.appVersion+'/js/'))

});


/*tag*/
gulp.task('tag', function () {
    gulp.src(buildRoot+"/**")
        .pipe(gulp.dest('../../tags/'+pkg.appName+"/build/"+pkg.appVersion));
});

/*build*/
gulp.task('default', function() {
    gulp.start('css', 'script',"html","images");
});

/*copy */
var copySrc = "src/**";
var copyRoot = "D:/www/MobileWorkflow/src/";
gulp.task('copy', function () {
    gulp.src([copySrc,"!../m/build","!../m/build/**/*","!../m/**/images","!../m/**/images/**/*","!../m/**/build.xml","!../m/**/node_modules","!../m/**/node_modules/**","!../m/**/node_modules/**/*"])
    // .pipe(replace(srcPath.base, "http://kxl.com/autostreets/"+pkg.appName+"/src"))
        .pipe(gulp.dest(copyRoot));
    // gulp.src("src/images/**").pipe(gulp.dest(copyRoot+"src/images"));
});
